using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Threading;

namespace Map2
{
    class user
    {
        private int x, y;
        private List<Building> buildings;
        private int mapWidth, mapHeight;
        private static Random rnd = new Random();
        private double angle = 0;
        private int speed;

        public user(List<Building> buildings, int width, int height, int speed)
        {
            this.speed = speed;
            x = rnd.Next(width);
            y = rnd.Next(height);
            angle = rnd.Next(6);
            this.buildings = buildings;

            foreach (Building b in buildings)
            {
                while (isInBuilding())
                {
                    x = rnd.Next(width);
                    y = rnd.Next(height);
                }
            }
            mapHeight = height;
            mapWidth = width;
        }

        public void nextCoordinate()
        {
            int oldX = x, oldY = y;
            x = oldX + (int)((double)speed * Math.Cos(angle)) + mapWidth;
            y = oldY + (int)((double)speed * Math.Sin(angle)) + mapHeight;
            x = x % mapWidth;
            y = y % mapHeight;

            while (isInBuilding())
            {
                angle += Math.PI / 6 * rnd.Next(12);
                x = oldX + (int)((double)speed * Math.Cos(angle)) + mapWidth;
                y = oldY + (int)((double)speed * Math.Sin(angle)) + mapHeight;
                x = x % mapWidth;
                y = y % mapHeight;
            }
        }

        private bool isInBuilding()
        {
            foreach (Building b in buildings)
            {
                if (b.isInPosition(x, y))
                    return true;
            }
            return false;
        }

        public Point getCoordinate()
        {
            return new Point(x, y);
        }



    }
}
